package com.xqboss.apps.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xqboss.apps.domain.user.UserIdentityPermWrenchProduction;
import com.xqboss.apps.domain.user.UserIdentityRank;
import com.xqboss.apps.domain.user.UserWrenchProductionRecord;
import com.xqboss.apps.dto.PageDto;
import com.xqboss.apps.dto.user.WrenchReferrerQueryDto;
import com.xqboss.apps.dto.user.WrenchReferrerRecordQueryDto;
import com.xqboss.apps.dto.user.upWrenchFactoryDto;
import com.xqboss.apps.service.user.UserAssetWrenchReceiveService;
import com.xqboss.apps.service.user.UserAssetWrenchService;
import com.xqboss.apps.service.user.UserIdentityPermWrenchProductionService;
import com.xqboss.apps.service.user.UserIdentityRankService;
import com.xqboss.apps.vo.user.*;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.core.domain.entity.SysUser;
import com.xqboss.common.exception.ServiceException;
import com.xqboss.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 用户资产-扳手
 * </p>
 *
 * @author 木鱼
 * @since 2023-12-07
 */
@RestController
@Slf4j
@Api(tags = "用户资产-扳手")
@RequestMapping("/user/asset/wrench")
public class UserAssetWrenchController extends BaseController {

    @Autowired
    private UserAssetWrenchService userAssetWrenchService;
    @Autowired
    private UserAssetWrenchReceiveService userAssetWrenchReceiveService;
    @Autowired
    private UserIdentityPermWrenchProductionService userIdentityPermWrenchProductionService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private UserIdentityRankService userIdentityRankService;

    @ApiOperation("首页详情")
    @GetMapping("/index")
    public R<WrenchIndexVo> index() {
        return R.ok(userAssetWrenchService.indexInfo(getUserId()));
    }

    @ApiOperation("直推分页列表")
    @GetMapping("/direct/page/list")
    public R<List<WrenchReferrerVo>> directPageList(@Validated WrenchReferrerQueryDto param) {
        param.startPage();
        param.setUserId(getUserId());
        return R.ok(userAssetWrenchService.getReferrerList(param));
    }

    @ApiOperation("直推贡献详情分页列表")
    @GetMapping("/direct/receive/page/list")
    public R<List<WrenchReferrerRecordVo>> directReceivePageList(@Validated WrenchReferrerRecordQueryDto param) {
        param.startPage();
        param.setUserId(getUserId());
        return R.ok(userAssetWrenchReceiveService.getReceiveList(param));
    }

    @ApiOperation("领取")
    @PostMapping("/receive")
    @RepeatSubmit(interval = 2000, justUrl = true, message = "请勿重复领取")
    public R<BigDecimal> receive() {
        return R.ok(userAssetWrenchService.receive(getUserId()));
    }

    /**
     * 列表查询
     */
    @GetMapping("/list")
    @ApiOperation(" 扳手生产套餐列表查询")
    public R<List<UserIdentityPermWrenchProduction>> loadList() {
        SysUser sysUser = sysUserService.lambdaQuery().select(SysUser::getIdentityRankId).eq(SysUser::getUserId,getUserId()).one();
        Long identityRankId = sysUser.getIdentityRankId();
        UserIdentityRank userIdentityRank = userIdentityRankService.lambdaQuery().eq(UserIdentityRank::getId, identityRankId).one();
        if (userIdentityRank == null || !userIdentityRank.getIsOpenWrenchProduction()) {
            throw new ServiceException("您的用户身份无法生产扳手");
        }
        List<UserIdentityPermWrenchProduction> list = userIdentityPermWrenchProductionService.appListByIdentityRankId(identityRankId);
        return R.ok(list);
    }

    /**
     * 启动扳手工厂
     * 扣除对应热豆,增加扳手工厂记录
     *
     * @param dto
     * @return
     */
    @ApiOperation("启动扳手工厂")
    @PostMapping("/upWrenchFactory")
    @RepeatSubmit(interval = 1000, justUrl = true, message = "请勿重复操作")
    public R upWrenchFactory(@Validated @RequestBody upWrenchFactoryDto dto) {
        return R.ok(userAssetWrenchService.upWrenchFactory(getUserId(), dto));
    }

    /**
     * 获取扳手累计产出 = 扳手记录已领取 + 此次已生产
     *
     * @return
     */
    @ApiOperation("获取扳手累计产出")
    @PostMapping("/getWrenchAccruing")
    @RepeatSubmit(interval = 1000, justUrl = true, message = "请勿重复操作")
    public R<getWrenchAccruingVo> getWrenchAccruing() {
        Long userId = getUserId();
        getWrenchAccruingVo vo = userAssetWrenchService.getWrenchAccruing(userId);
        return R.ok(vo);
    }

    @ApiOperation("扳手生产领取")
    @PostMapping("/wrenchFactoryReceiveAward")
    @RepeatSubmit(interval = 1000, justUrl = true, message = "请勿重复操作")
    public R wrenchFactoryReceiveAward() {
        Long userId = getUserId();
        WrenchFactoryReceiveAwardVo vo = userAssetWrenchService.wrenchFactoryReceiveAward(userId);
        return R.ok(vo);
    }

    @ApiOperation("扳手生产领取记录")
    @PostMapping("/wrenchFactoryRecord")
    @RepeatSubmit(interval = 1000, justUrl = true, message = "请勿重复操作")
    public R<List<UserWrenchProductionRecord>> wrenchFactoryRecord(@RequestBody PageDto dto) {
        Long userId = getUserId();
        Page<UserWrenchProductionRecord> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<UserWrenchProductionRecord> vo = userAssetWrenchService.wrenchFactoryRecord(page,userId);
        return R.ok(vo.getRecords(),vo.getTotal());
    }

    /**
     * 扳手生产总领取金额
     * @param
     * @return
     */
    @ApiOperation("扳手生产总领取金额")
    @GetMapping("/wrenchFactoryGiveAmount")
    @RepeatSubmit(interval = 1000, justUrl = true, message = "请勿重复操作")
    public R<BigDecimal> wrenchFactoryGiveAmount() {
        Long userId = getUserId();
        BigDecimal amount = userAssetWrenchService.wrenchFactoryGiveAmount(userId);
        return R.ok(amount);
    }



    /**
     * 获取本次生产剩余时间和状态
     *
     * @return
     */
    @ApiOperation("获取本次订单生产剩余时间和状态")
    @PostMapping("/getProductionStatusAndTime")
    @RepeatSubmit(interval = 1000, justUrl = true, message = "请勿重复操作")
    public R<WrenchGetProductionStatusAndTimeVo> getProductionStatusAndTime() {
        Long userId = getUserId();
        WrenchGetProductionStatusAndTimeVo vo = userAssetWrenchService.getProductionStatusAndTime(userId);
        return R.ok(vo);
    }

}
